import wx
import wx.media
from Constant import *
from PublicMethod import *
'''
Frame_Option : User can set whether playing sound of music and effect sound. In addition,

               user can see ranking through clicking label - Check Rank. Finally, by clicking
               
               Main Menu, user can leave option and return to the main menu. All control through
               
               keyboard, the key - up and down,user can select title setting. In each title,
               
               user can change selection by clicking left and right key. If the title is the function
               
               to turn to other graph, please click Enter key.
'''
class Frame_Option(wx.Panel):
    '''
    Constructor : Frame_Option
        parent : the main frame of this
    '''
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1, size = (parent.GetSize()[0], parent.GetSize()[1] - 20))
        self._001_initialInstanceVariable(parent)
        self._002_bindAction()
        self._003_createBackground()
        self._004_createTextIamge()
        self._005_createOptionLabel()
        self.ACT001_setRowSelection(0, True)
    '''
    function _001 : initialize the variables used for this object
        main : the main frame
    '''
    def _001_initialInstanceVariable(self, main):
        #pointer of main frame
        self.main = main
        #constant of title
        self.MUSIC = 0
        self.SOUND = 1
        self.RANK = 2
        self.EXIT = 3
        #directory name of storing image of option
        self.dirOptionImg = "img option"
        #list of selection image
        self.listImgTitle = [["img_music.png"],
                             ["img_effectSound.png"],
                             ["img_checkRank1.png", "img_checkRank2.png"],
                             ["img_exit1.png", "img_exit2.png"]]
        #initial value
        if self.main.sound != None :
            self.listInitValue = [self.main.sound.GET002_isPlayBackground(),
                                  self.main.sound.GET001_isPlayEffect()]
        else :
            self.listInitValue = [False, False]
        #list of on and off image
        self.listImgOnOff = {'on' : ["img_on1.png", "img_on2.png"],
                             'off' : ["img_off1.png", "img_off2.png"]}
        #list of title image object
        self.listImgObj = []
        #image object of title
        self.titleImage = None
        #record current selection of title bar
        self.curSelRow = self.MUSIC
    '''
    function _002 : bind object event to specific method
    '''
    def _002_bindAction(self):
        self.main.Bind(wx.EVT_KEY_DOWN, self.SLOT001_OnKeyClick)
    '''
    function _003 : create background image of this frame
    '''
    def _003_createBackground(self):
        image = PublicMethod.ACT001_getBmpImage(FILE_NAME_BACKGROUND, self.GetSize()[0], self.GetSize()[1])
        self.imgBackground = wx.StaticBitmap(parent=self, bitmap=image)
        self.imgBackground.Show(False)
        self.imgBackground.Show(True)
    '''
    function _004 : create text image of all title and selection
    '''
    def _004_createTextIamge(self):
        pos = [90,140]
        interval = 120
        index = 0
        for imageName in self.listImgTitle :
            #create title
            fileName = self.dirOptionImg + "/" + imageName[0]
            image = PublicMethod.ACT001_getBmpImage(fileName)
            label = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
            label.Enable(False)
            appendObj = [label]
            if index < 2:  #this index of title is selection, append on/off text
                isOn = self.listInitValue[index]
                if isOn :
                    fileName = self.dirOptionImg + "/" + self.listImgOnOff['on'][0]
                else :
                    fileName = self.dirOptionImg + "/" + self.listImgOnOff['off'][0]
                image = PublicMethod.ACT001_getBmpImage(fileName)
                label = wx.BitmapButton(self, -1, image, pos = (pos[0] + 500, pos[1]), style = wx.FRAME_SHAPED)
                label.Enable(False)
                appendObj.append(label)
                appendObj.append(isOn)
            self.listImgObj.append(appendObj)
            pos[1] = pos[1] + interval
            index = index + 1
    '''
    function _005 : create title label of option
    '''
    def _005_createOptionLabel(self):
        fileName = self.dirOptionImg + "/img_optionSetting.png"
        image = PublicMethod.ACT001_getBmpImage(fileName)
        pos = ((self.GetSize()[0] - image.GetSize()[0]) / 2, 10)
        self.titleImage = wx.BitmapButton(self, -1, image, pos = pos, style = wx.FRAME_SHAPED)
        self.titleImage.Enable(False)
    '''
    function _006 : set current selection to object - main
    '''
    def _006_setSelectionToMain(self):
        if self.main.sound != None :
            self.main.sound.SET001_isPlayEffect(self.listImgObj[self.SOUND][2])
            self.main.sound.SET002_isPlayBackground(self.listImgObj[self.MUSIC][2])
    '''
    function _007 : destroy all object of this
    '''
    def _007_destroy(self):
        #clear title image
        self.titleImage.Destroy()
        #clear all selection
        index = 0
        while index < self.listImgTitle.__len__() :
            self.listImgObj[index][0].Destroy()
            if index < 2:
                self.listImgObj[index][1].Destroy()
            index = index + 1
        #clear background
        self.imgBackground.Destroy()
        self.Destroy()
    '''
    function _GameFunc001 : open another panel of Frame_Rank
    '''
    def _GameFunc001_openRank(self):
        #set current selection in each title to main
        self._006_setSelectionToMain()
        main = self.main
        self._007_destroy()
        from Frame_Rank import Frame_Rank
        Frame_Rank(main)
    '''
    function _GameFunc002 : close this panel and open Frame_Main
    '''
    def _GameFunc002_returnMainMenu(self):
        #set current selection in each title to main
        self._006_setSelectionToMain()
        main = self.main
        #destroy
        self._007_destroy()
        #create object - Frame_Main
        from Frame_Main import Frame_Main
        Frame_Main(main)
    '''
    function ACT001 : set current title selection
        index : index of title
        isSelect : set title in index to isSelect(True / False)
    '''
    def ACT001_setRowSelection(self, index, isSelect):
        setLabel = None
        setIndex = 0
        if isSelect :
            setIndex = 1
        if index < 2:  #this title is on/off selection
            if self.listImgObj[index][2] == True :  #this selection of this title is 'on'
                fileName = self.dirOptionImg + "/" + self.listImgOnOff['on'][setIndex]
                image = PublicMethod.ACT001_getBmpImage(fileName)
                setLabel = self.listImgObj[index][1]
                self.listImgObj[index][1].SetBitmapLabel(image)
            else :  #this selection of this title is 'off'
                fileName = self.dirOptionImg + "/" + self.listImgOnOff['off'][setIndex]
                image = PublicMethod.ACT001_getBmpImage(fileName)
                setLabel = self.listImgObj[index][1]
                self.listImgObj[index][1].SetBitmapLabel(image)
        else :  #this title is "Enter"
            fileName = self.dirOptionImg + "/" + self.listImgTitle[index][setIndex]
            image = PublicMethod.ACT001_getBmpImage(fileName)
            self.listImgObj[index][0].SetBitmapLabel(image)
            setLabel = self.listImgObj[index][0]
        setLabel.Show(False)
        setLabel.Show(True)
    '''
    function ACT002 : set current title in passing index to next selection
        index : index of title
        isSelect : set title in index to isSelect(True / False)
    '''
    def ACT002_setColSelectionNext(self, index, isSelect):
        setIndex = 0
        if isSelect :
            setIndex = 1
        self.listImgObj[index][2] = (not self.listImgObj[index][2])
        if self.listImgObj[index][2] == True :  #set selection of this title to 'on'
            fileName = self.dirOptionImg + "/" + self.listImgOnOff['on'][setIndex]
        else :  #set selection of this title to 'off'
            fileName = self.dirOptionImg + "/" + self.listImgOnOff['off'][setIndex]
        image = PublicMethod.ACT001_getBmpImage(fileName)
        self.listImgObj[index][1].SetBitmapLabel(image)
        self.listImgObj[index][1].Show(False)
        self.listImgObj[index][1].Show(True)
    '''
    function SLOT001 : the slot of clicking on keyboard
    '''
    def SLOT001_OnKeyClick(self, event):
        key = event.GetKeyCode()
        if key == wx.WXK_UP :  #press key - Up
            self.ACT001_setRowSelection(self.curSelRow, False)
            self.curSelRow = self.curSelRow - 1
            if self.curSelRow < 0 :
                self.curSelRow = self.listImgTitle.__len__() - 1
            self.ACT001_setRowSelection(self.curSelRow, True)
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_DOWN :  #press key - Down
            self.ACT001_setRowSelection(self.curSelRow, False)
            self.curSelRow = self.curSelRow + 1
            if self.curSelRow > self.listImgTitle.__len__() - 1 :
                self.curSelRow = 0
            self.ACT001_setRowSelection(self.curSelRow, True)
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_LEFT :  #press key - Left
            if self.curSelRow < 2 :
                self.ACT002_setColSelectionNext(self.curSelRow, True)
                self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == wx.WXK_RIGHT :  #press key - right
            if self.curSelRow < 2 :
                self.ACT002_setColSelectionNext(self.curSelRow, True)
                self.main.ACT001_playSound(SOUND_SELECT)  #play sound
        elif key == 13 :  #press key - Enter
            if self.curSelRow == self.RANK :
                self.main.ACT001_playSound(SOUND_ENTER)  #play sound
                self._GameFunc001_openRank()
            elif self.curSelRow == self.EXIT :
                self.main.ACT001_playSound(SOUND_ENTER)  #play sound
                self._GameFunc002_returnMainMenu()
        elif key == wx.WXK_ESCAPE :  #click key - Esc
            self.ACT001_setRowSelection(self.curSelRow, False)
            self.curSelRow = self.EXIT
            self.ACT001_setRowSelection(self.curSelRow, True)
            self.main.ACT001_playSound(SOUND_SELECT)  #play sound
'''
main program
'''
if __name__ == "__main__" :
    app = wx.PySimpleApp()
    from Main import Main
    frame = Main()
    Frame_Option(frame)
    frame.Show()
    app.MainLoop()